﻿using ClockInLearning.Db;
using ClockInLearning.Dto;
using ClockInLearning.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ClockInLearning.Impl
{
    public class UserRelationApplication
    {
        private readonly AppDbContext _context;

        public UserRelationApplication(AppDbContext context)
        {
            _context = context;
        }

        public async Task<ServiceResult> AddSupervisor(UserRelationInputDto dto)
        {
            var result = new ServiceResult();

            // 从token获取用户信息
            var tokenInfo = CallContext.GetData("token") as TokenInfo;
            var userInfo = _context.UserInfos.Where(x => x.Id == tokenInfo.UserId).First().SimpleMapTo<UserInfoOutputDto>();

            // 校验是否已添加
            var hasCheckFlag = _context.UserRelations.Where(x => x.UserId == userInfo.Id && x.SupervisorId == dto.SupervisorId).Any();
            if (hasCheckFlag)
            {
                result.IsFailed("该账号已添加");
                return result;
            }

            // 添加
            var newSupervisor = dto.SimpleMapTo<UserRelation>();
            newSupervisor.Id = Guid.NewGuid();
            newSupervisor.UserId = userInfo.Id;
            newSupervisor.IsDelete = false;
            await _context.AddAsync(newSupervisor);
            await _context.SaveChangesAsync();

            result.IsSuccess();
            return result;
        }

        public async Task<ServiceResult> Update(UserRelationInputDto dto)
        {
            var result = new ServiceResult();

            // 从token获取用户信息
            var tokenInfo = CallContext.GetData("token") as TokenInfo;
            var userInfo = _context.UserInfos.Where(x => x.Id == tokenInfo.UserId).First().SimpleMapTo<UserInfoOutputDto>();

            // 获取需要更新的数据
            var oldData = _context.UserRelations.Where(x => x.UserId == userInfo.Id && x.Id == dto.Id).FirstOrDefault();
            if (oldData == null)
            {
                result.IsFailed("更新数据不存在");
                return result;
            }

            oldData.IsDelete = dto.IsDelete;
            await _context.SaveChangesAsync();

            result.IsSuccess();
            return result;
        }

        public async Task<ServiceResult<List<UserRelationOutputDto>>> GetSupervisedList()
        {
            var result = new ServiceResult<List<UserRelationOutputDto>>();
            var thisUser = _context.GetUserInfoByToken<UserInfo>();
            var returnLst = _context.UserRelations.Where(x => x.SupervisorId == thisUser.Id)?.Select(x => x.SimpleMapTo<UserRelationOutputDto>()).ToList();
            foreach (var item in returnLst)
            {
                var user = _context.UserInfos.Where(x => x.Id == item.UserId).Select(x => x.SimpleMapTo<UserListDto>()).FirstOrDefault();
                item.UserName = user.UserName;
                item.UserAvatar = user.Avatar;
                item.UserAccount = user.Account;
                item.SupervisorName = thisUser.UserName;
                item.SupervisorAvatar = thisUser.Avatar;
                item.SupervisorAccount = thisUser.Account;
            }
            result.IsSuccess(returnLst);
            return result;
        }

        public async Task<ServiceResult<List<UserRelationOutputDto>>> GetSupervisorList()
        {
            var result = new ServiceResult<List<UserRelationOutputDto>>();
            var thisUser = _context.GetUserInfoByToken<UserInfo>();
            var returnLst = _context.UserRelations.Where(x => x.UserId == thisUser.Id)?.Select(x => x.SimpleMapTo<UserRelationOutputDto>()).ToList();
            foreach (var item in returnLst)
            {
                var supervisor = _context.UserInfos.Where(x => x.Id == item.SupervisorId).Select(x => x.SimpleMapTo<UserListDto>()).FirstOrDefault();
                item.UserName = thisUser.UserName;
                item.UserAvatar = thisUser.Avatar;
                item.UserAccount = thisUser.Account;
                item.SupervisorName = supervisor.UserName;
                item.SupervisorAvatar = supervisor.Avatar;
                item.SupervisorAccount = supervisor.Account;
            }
            result.IsSuccess(returnLst);
            return result;
        }
    }
}
